Title: Bayesian for Hackers in R Slug: bayesian-for-hackers-r Summary: An implementation of the book Bayesian for Hackers, converting the code from Python to R Date: 2018-01-22 10:47 Category: Statistics Tags: Bayesian Authors: Thomas Pinder
Bayesian statistics is a topic that has always interested me. The notion of defining prior beliefs, then updating these belief based upon the data feels a more natural approach to statistics than the frequentist approach. That being said, I have unfortunately not had the chance to formally study this topic in any great depth until now, often just looking into a specific aspects of Bayesian statistics as and when they were needed in my work.
The posts here try and replicate the work done within the Bayesian for Hackers book, converting the code there into R so as to force me to understand exactly what is happening.
In [1]:
library(ggplot2)
set.seed(4)
n_trials <- c(0, 1, 2, 3, 4, 5, 8, 15, 50, 500)
x <- seq(0, 1, length.out = 100)
# Generate Bernoulli Samples
data <- rbinom(n = tail(n_trials,1), size = 1, p = 0.5)
par(mfrow = c(2,2))
# Simulate now using Binomial's conjugate prior
for (i in 1:length(n_trials)){
heads = sum(data[1:n_trials[i]])
y = dbeta(x, 1+heads, 1+n_trials[i]-heads)
plot(x, y, type = "l")
legend("topright", c(paste(n_trials[i], " Tosses. ", sep = ""), paste(heads, " Heads.", sep = "")))
}
In [16]: